import router from '@/router';
import { onMounted, onUnmounted, reactive, watch } from 'vue';
import { queryList, XiaFa } from '@api/url/equipmentManagement';
import notification from '@arco-design/web-vue/es/notification';

export default function importConfig() {
  const base = reactive<any>({
    config1: {
      header: ['设备名称', '米数', '状态', '操作'],
      data: [],
      align: ['center', 'center', 'center', 'center'],
    },
    config2: {
      header: ['设备名称', '计数产量', '状态', '操作'],
      data: [],
      align: ['center', 'center', 'center', 'center'],
    },
    resetVisible: false,
    resetId: null,
    know: null,
    intervalId: null,
  });

  const back = () => {
    router.go(-1);
  };

  //显示清零弹窗
  const showReset = ({ row, ceil }) => {
    if (ceil == '清零') {
      let know;
      if (row[0].split('-')[0] == '压铸机') {
        know = 1;
      } else {
        know = 0;
      }
      base.resetVisible = true;
      base.resetId = row[0].split('-')[1];
      base.know = know;
    }
  };
  //清零
  const reset = () => {
    base.resetVisible = false;
    XiaFa({ id: base.resetId, know: base.know }).then((res) => {
      if (res.code == 200) notification.info('清零成功');
      else notification.info('清零失败');
      getData();
    });
  };
  //关闭清零弹窗
  const cancel = () => {
    base.resetVisible = false;
  };

  //获取数据
  const getData = () => {
    queryList().then((res: any) => {
      if (res.code == '200') {
        //大屏数据
        const arrTotal = [];
        let arr;
        res.data.forEach((item) => {
          arr = [item.reset + '-' + item.id, item.count, item.length, item.status];
          arrTotal.push(arr);
        });

        base.config1.data = [];
        base.config2.data = [];
        arrTotal.forEach((item) => {
          arr = [];
          if (item[0].split('-')[0] == '压铸机') {
            item.forEach((item1, index1) => {
              if (item1) {
                arr.push(item1);
              }
            });
            arr.push('清零');
            base.config1.data.push(arr);
          } else if (item[0].split('-')[0] == '注塑机') {
            item.forEach((item2, index2) => {
              if (item2 || item2 == 0) {
                arr.push(item2);
              }
            });
            arr.push('清零');
            base.config2.data.push(arr);
          }
        });
      }
    });
  };

  onMounted(() => {
    getData();

    // 创建定时器，每5秒执行一次
    base.intervalId = setInterval(function () {
      getData();
    }, 5000);
  });

  onUnmounted(() => {
    clearInterval(base.intervalId);
  });

  return {
    base,
    back,
    reset,
    showReset,
    cancel,
  };
}
